<script setup>
import {ref, reactive,onMounted,onBeforeUnmount} from 'vue'
// api

// router
import {useRouter} from 'vue-router'
import {useStore} from 'vuex'
// use cookie
import {setToken} from '~/composables/auth.js'
// 通知工具
import {notify} from "~/composables/utils.js";

const store = useStore()
const router = useRouter()

const form = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [
    {required: true, message: '用户名不能为空', trigger: 'blur'},
    {min: 3, max: 10, message: '长度在3到10之间', trigger: 'blur'}
  ],
  password: [
    {required: true, message: '密码不能为空', trigger: 'blur'}
  ]
}

const formRef = ref(null)
const loading = ref(false)
const handleSubmit = () => {
  formRef.value.validate(valid => {
    if (!valid) {
      return false
    }
    loading.value = true
    store.dispatch('login', form).then(res => {
      notify('登录成功')
      router.push("/")
    }).finally(() => {
      loading.value = false
    })
  })
}
// 监听键盘事件
const handleKeyup = (e) => {
  if (e.key === 'Enter') {
    handleSubmit()
  }
}
// 监听
onMounted(() => {
  document.addEventListener('keyup', handleKeyup)
})
// 移除监听
onBeforeUnmount(() => {
  document.removeEventListener('keyup', handleKeyup)
})


</script>

<template>
  <el-row class="login-container">
    <el-col :lg="16" :md="12" class="left">
      <div>
        <div>欢迎光临</div>
        <div>秒购商城后台</div>
      </div>
    </el-col>
    <el-col :lg="8" :md="12" class="right">
      <h2 class="title">欢迎回来秒购后台</h2>
      <div>
        <span class="line"></span>
        <span>账号密码登录</span>
        <span class="line"></span>
      </div>
      <el-form ref="formRef" :model="form" class="w-[250px]" :rules="rules">
        <el-form-item prop="username">
          <el-input placeholder="请输入账号" v-model="form.username">
            <template #prefix>
              <el-icon class="el-input__icon">
                <User/>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input placeholder="请输入密码" v-model="form.password" show-password>
            <template #prefix>
              <el-icon>
                <Lock/>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleSubmit" :loading="loading" class="w-[250px]" round color="#f59e0b" type="primary">
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<style scoped>
.login-container {
  @apply min-h-screen bg-yellow-500
}

.login-container .left, .login-container .right {
  @apply flex items-center justify-center
}

.login-container .right {
  @apply bg-light-50 flex-col
}

.left > div > div:first-child {
  @apply font-bold text-5xl text-light-50 mb-4
}

.left > div > div:last-child {
  @apply text-light-50
}

.right .title {
  @apply font-bold text-3xl text-gray-800
}

.right > div {
  @apply flex items-center justify-center my-5 text-gray-400 space-x-2
}

.right .line {
  @apply h-[1px] w-16 bg-gray-200
}
</style>